//
//  UserAccount.swift
//  SinaWeibo
//
//  Created by 张子国 on 15/11/11.
//  Copyright © 2015年 张子国. All rights reserved.
//

import UIKit

class UserAccount: NSObject,NSCoding {
    var access_token: String?
    
    
    var expires_in: NSTimeInterval = 0 {
        didSet {
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    //过期日期  开发者的过期时间 是 5年  普通用户的过期时间是 3天
    var expires_date: NSDate?

    var uid: String?
    
    var avatar_large: String?
  
    var name: String?
    
    init(dict:[String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
        
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    //重绘
    override var description: String {
        let keys = ["access_token","expires_in","uid","avatar_large","name","expires_date"]
        
        // let keys = ["access_token","expires_in","uid","avatar_large","name"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
    //验证归档
    func saveAccount() {
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
       // print(path)
    }

    //对外提供届归档方法
    //func  是一个对象方法  函数前 加 class
    class func loadAccount() -> UserAccount? {
        //解归档
        //给string对象 变成 路径的这个方法 在 swift 2.0  被 搞丢了
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? UserAccount {
            return account
        //判断用户的token是否过期
        if account.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                return account
            }
            
        }
        return nil
    }
    
    
    //MARK: 实现NSCoding 协议方法
    //解归档  将 磁盘中的二进制数据 -> 转换成自定义对象  跟反序列化很像
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeObjectForKey("uid") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        name = aDecoder.decodeObjectForKey("name") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
    }
    
    //归档 将对象 转换二进制  保存到磁盘中  序列化
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(expires_date, forKey: "expires_date")

        
    }


    }


